package tests;


import commons.Utils;
import io.github.bonigarcia.wdm.WebDriverManager;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.io.IOException;
import java.time.Duration;

/**
 * Created with IntelliJ IDEA.
 * Description :
 * User: jiume
 * Date: 2025-09-07
 * Time: 18:45
 */
// 音乐列表页面UI自动化测试
public class ListPage {

    public static final String url = "http://134.175.206.246:9090/music.html";

    public static WebDriver driver;

    public ListPage() throws IOException {
        // 1、创建浏览器驱动对象
        driver = createWebDriver();
        // 2、设置隐式等待
        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(2));
        // 2、访问列表页面
        accessListPage(url);
    }


    /**
     * 检查页面元素是否可以正常显示
     */
    public void checkListPageRight() throws IOException {
        String app_title = driver.findElement(By.cssSelector("#body > div.container > h1")).getText();
        String placeholder = driver.findElement(By.cssSelector("#exampleInputName2")).getAttribute("placeholder");
        String search = driver.findElement(By.cssSelector("#submit1")).getText();
        String love = driver.findElement(By.cssSelector("#body > div.container > div.search-section > div > a.btn.btn-outline")).getText();
        String add = driver.findElement(By.cssSelector("#body > div.container > div.search-section > div > a.btn.btn-primary")).getText();
        String delete = driver.findElement(By.cssSelector("#delete")).getText();
        String select = driver.findElement(By.cssSelector("#body > div.container > table > thead > tr > th:nth-child(1)")).getText();
        String song = driver.findElement(By.cssSelector("#body > div.container > table > thead > tr > th:nth-child(2)")).getText();
        String singer = driver.findElement(By.cssSelector("#body > div.container > table > thead > tr > th:nth-child(3)")).getText();
        String operation = driver.findElement(By.cssSelector("#body > div.container > table > thead > tr > th:nth-child(4)")).getText();

//        System.out.println(app_title);
//        System.out.println(placeholder);
//        System.out.println(search);
//        System.out.println(love);
//        System.out.println(add);
//        System.out.println(delete);
//        System.out.println(select);
//        System.out.println(song);
//        System.out.println(singer);
//        System.out.println(operation);

        assert "在线音乐服务器".equals(app_title) && "输入歌曲名称搜索...".equals(placeholder)
                && "搜索".equals(search) && "喜欢列表".equals(love) && "添加歌曲".equals(add)
                && "删除选中".equals(delete) && "选择".equals(select) & "歌名".equals(song)
                && "歌手".equals(singer) && "操作".equals(operation);

        Utils.getScreenShot(driver, "checkListPageRight");
    }


    /**
     * 模拟删除音乐
     */
    public void deleteMusic() throws IOException {
        // 点击删除按钮
        driver.findElement(By.cssSelector("#info > tr:nth-child(1) > td.action-buttons > button:nth-child(2)")).click();
        // 等待弹窗出现
        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(2));
        wait.until(ExpectedConditions.alertIsPresent());
        // 切换到弹窗
        Alert alert = driver.switchTo().alert();
        String warning = alert.getText();
        System.out.println(warning);
        assert "删除成功!".equals(warning);
        alert.accept();

        Utils.getScreenShot(driver, "deleteMusic");
    }


    /**
     * 模拟上传歌曲
     */
    public void uploadMusic() throws IOException {
        // 点击跳转链接
        driver.findElement(By.cssSelector("#body > div.container > div.search-section > div > a.btn.btn-primary")).click();

        // 上传音乐文件
        // 1、找到文件上传按钮
        // 2、输入文件路径
        String musicPath = "C:\\Users\\jiume\\Desktop\\在线音乐服务器—音乐文件\\汪源 - 北京东路的日子.mp3";
        // 这里要定位到 <input type=file> 标签，该标签才是用来上传文件的
        driver.findElement(By.cssSelector("#uploadForm > div:nth-child(1) > div > label > input")).sendKeys(musicPath);

        // 填写歌手名
        driver.findElement(By.cssSelector("#uploadForm > div:nth-child(2) > input")).sendKeys("test123456");

        // 点击上传
        driver.findElement(By.cssSelector("#uploadForm > button")).click();

        // 等待弹窗出现
        // 由于服务器配置一般，得等待较长时间
        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(60));
        wait.until(ExpectedConditions.alertIsPresent());

        // 切换到弹窗
        Alert alert = driver.switchTo().alert();
        String warning = alert.getText();
//        System.out.println(warning);
        assert "上传成功！".equals(warning);
        alert.accept();

        Utils.getScreenShot(driver, "uploadMusic");
    }


    /**
     * 模拟收藏音乐、取消收藏
     */
    public void collectionMusic() throws IOException {
        // 收藏
        driver.findElement(By.cssSelector("#info > tr:nth-child(1) > td.action-buttons > button:nth-child(3)")).click();

        // 等待弹窗出现
        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(2));
        wait.until(ExpectedConditions.alertIsPresent());

        // 切换到弹窗
        Alert alert = driver.switchTo().alert();
        String warning = alert.getText();
//        System.out.println(warning);
        assert "收藏成功！".equals(warning);
        alert.accept();

        // 跳转到喜欢列表
        driver.findElement(By.cssSelector("#body > div.container > div.search-section > div > a.btn.btn-outline")).click();

        Utils.getScreenShot(driver, "collectionMusic");

        // 取消收藏
        driver.findElement(By.cssSelector("#info > tr > td.action-buttons > button.btn.btn-outline")).click();

        // 等待弹窗出现
        wait.until(ExpectedConditions.alertIsPresent());

        // 切换到弹窗
        alert = driver.switchTo().alert();
        warning = alert.getText();
//        System.out.println(warning);
        assert "取消收藏成功!".equals(warning);
        alert.accept();

        // 回到首页
        driver.findElement(By.cssSelector("#body > div.container > div.search-section > div > a")).click();
    }


    /**
     * 模拟播放的场景
     */
    public void playSong() throws IOException {
        driver.findElement(By.cssSelector("#info > tr:nth-child(1) > td.action-buttons > button.btn.btn-primary")).click();
        Utils.getScreenShot(driver, "playSong");
    }


    /**
     * 模拟搜索的场景
     */
    public void search() throws IOException {
        // 针对关键词-模糊查询
        driver.findElement(By.cssSelector("#exampleInputName2")).sendKeys("天后");
        driver.findElement(By.cssSelector("#submit1")).click();
        Utils.getScreenShot(driver, "search_天后");

        // 查询所有
        driver.findElement(By.cssSelector("#exampleInputName2")).clear();
        driver.findElement(By.cssSelector("#submit1")).click();
        Utils.getScreenShot(driver, "search_all");
    }


    /**
     * 访问列表页面
     * @param url 列表页面的URL
     */
    private void accessListPage(String url) throws IOException {
        driver.get(url);
        // 先得等待弹窗出现
        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(2));
        wait.until(ExpectedConditions.alertIsPresent());
        // 切换到弹窗，并点击确认弹窗
        Alert alert = driver.switchTo().alert();
        String warning = alert.getText();
//        System.out.println(warning);
        assert "用户未登录".equals(warning);
        alert.accept();
        // 执行登录逻辑
        driver.findElement(By.cssSelector("#username")).sendKeys("test123456");
        driver.findElement(By.cssSelector("#password")).sendKeys("123456");
        driver.findElement(By.cssSelector("#loginForm > button")).click();
        // 等待弹窗出现
        wait.until(ExpectedConditions.alertIsPresent());
        // 切换到弹窗，并点击确认弹窗
        alert = driver.switchTo().alert();
        warning = alert.getText();
//        System.out.println(warning);
        assert "登录成功! 欢迎回来!".equals(warning);
        alert.accept();
//        Utils.getScreenShot(driver, "accessListPage");
    }


    /**
     * 创建浏览器驱动对象
     * @return 返回浏览器驱动对象
     */
    private WebDriver createWebDriver() {
        if (driver == null) {
            // 1、下载浏览器驱动
            WebDriverManager.chromedriver().setup();
            // 2、添加配置：
            // 允许访问所有链接
            ChromeOptions options = new ChromeOptions();
            options.addArguments("--remote-allow-origins=*");
            // 也可以禁用自动化提示条
            options.setExperimentalOption("excludeSwitches", new String[]{"enable-automation", "enable-logging"});
            options.setExperimentalOption("useAutomationExtension", false);
            // 3、创建浏览器驱动对象
            driver = new ChromeDriver(options);
        }
        return driver;
    }


    /**
     * 退出浏览器
     */
    public void quit() {
        driver.quit();
    }
}
